package com.ipt.frm.pw;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ipt.frm.pw.bean.Mensaje;
import com.ipt.frm.pw.enumeracion.TipoMensaje;

/**
 * Servlet implementation class ServletMaestro
 */
public class ServletMaestro extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	static final String INIT_PARAM_USER_NAME = "username";
	static Properties propertiesMsg;
	static final String PATH_PROPERTIES_MSG = "/WEB-INF/config/messages.properties";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletMaestro() {
        super();
   
    }
    
    public void init(ServletConfig config) throws ServletException{
    	
    	super.init(config);
    	
    	propertiesMsg = new Properties();
    	
    	
    		//propertiesMsg.load(getServletContext().getResourceAsStream(PATH_PROPERTIES_MSG));
        propertiesMsg =(Properties) getServletContext().getAttribute("properties");
    	
    }
    
    
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    	
     	
    	HttpSession session = request.getSession();
    	Mensaje mensaje;
    	
    	String user =(String) session.getAttribute(INIT_PARAM_USER_NAME);
    	 
    	 if(user==null){
    		mensaje = new Mensaje(TipoMensaje.ALERT, propertiesMsg.getProperty("servlet.Masterno.user"));
    		//request.setAttribute("MSG", "No hay session almacenada previa por favor ingrese nombre y password");
  			request.setAttribute("tittle", "Login");
    		request.setAttribute("MSG",mensaje);
    		RequestDispatcher dispacher = this.getServletContext().getRequestDispatcher("/login.jsp");
  			dispacher.forward(request, response);
    	 }
    	 else{
    		 super.service(request, response);
    	 }
    	 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		propertiesMsg = null;
	}

}
